<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package}.dao.mybatis.${ClassName}Mapper">

  <resultMap type="${package}.entity.${moduleName}.${ClassName}" id="${className}Map">
    <#list fieldList as field>
      <result property="${field.attrName}" column="${field.fieldName}"/>
    </#list>
  </resultMap>

  <sql id="Base_Column_List">
      <#list fieldList as field>
        t.${field.fieldName}<#if field_has_next>,</#if>
      </#list>
  </sql>

    <#-- 根据主键查询数据 -->
  <select id="selectByPrimaryKey"  resultType="${package}.entity.${moduleName}.${ClassName}" parameterType="java.lang.Integer">
    select
    <include refid="Base_Column_List"/>
    from ${tableName} t
    where t.id = <#noparse>#</#noparse>{id}
  </select>

    <#-- 根据主键逻辑删除数据 -->
  <update id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    UPDATE ${tableName}
    SET
    is_delete = 1
    WHERE id = <#noparse>#</#noparse>{id}
  </update>

  <update id="changeStatusById">
    update ${tableName}
    SET
    status =  <#noparse>#</#noparse>{status}
    WHERE id =  <#noparse>#</#noparse>{id}
  </update>

  <!--新增或更新数据 有id进行更新，没有id就进行新增。-->
  <insert id="saveOrUpdate" keyProperty="id" useGeneratedKeys="true" parameterType="${package}.entity.${moduleName}.${ClassName}">
    <if test='params.id == null'>
        insert into
          ${tableName} t
      <trim prefix="(" suffix=")" suffixOverrides="," >
        <#list fieldList as field>
          <#if field.attrName != 'id'>
              <if test='params.${field.attrName} != null and params.${field.attrName} != "" '>
                t.${field.fieldName},
              </if>
          </#if>
            <#if field.attrName == 'version'>
              version
            </#if>
        </#list>
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides="," >
        <#list fieldList as field>
          <#if field.attrName != 'id'>
            <if test='params.${field.attrName} != null and params.${field.attrName} != "" '>
                <#noparse>#</#noparse>{params.${field.attrName}},
            </if>
          </#if>
          <#if field.attrName == 'version'>
            1
          </#if>
        </#list>
      </trim>
    </if>

    <if test='params.id != null'>
        UPDATE ${tableName}
        <set>
            <#list fieldList as field>
              <if test='params.${field.attrName} != null and params.${field.attrName} != "" '>
                  ${field.fieldName} = <#noparse>#</#noparse>{params.${field.attrName}},
              </if>
            </#list>
        </set>
        WHERE id = <#noparse>#</#noparse>{id}
      </if>
  </insert>

  <#-- 根据Dto分页查询 -->
  <select id="query${ClassName}ListByCondition"
          parameterType="${package}.dto.api.${moduleName}.Get${ClassName}ParamsDto"
          resultType="${package}.entity.${moduleName}.${ClassName}">
    select
      <#list fieldList as field>
          t.${field.fieldName} as ${field.attrName}<#if field_has_next>,</#if>
      </#list>
    FROM
      ${tableName} t
    <where>
        <#list fieldList as field>
            <#if field.attrName != 'createTime' &&
                     field.attrName != 'updateTime'&&
                     field.attrName != 'updateUserId'&&
                     field.attrName != 'createUserId'&&
                     field.attrName != 'isDelete'&&
                     field.attrName != 'version'&&
                     field.attrName != 'id'
                     >
              <#if field.attrType == 'String'>
      <if test='params.${field.attrName} != null and params.${field.attrName} != "" '>
        and t.${field.fieldName} like concat('%',  <#noparse>#</#noparse>{params.${field.attrName}}, '%')
              <#else>
      <if test='params.${field.attrName} != null'>
        and t.${field.fieldName} = <#noparse>#</#noparse>{params.${field.attrName}}
              </#if>
      </if>
      </#if>
        </#list>
    </where>
    order by create_time desc
  </select>


</mapper>
